clear all;


specieslist = {'Ciona'; 'Arbacia'; 'Lpictus'; 'Lvariegatus';...
    'Spurpuratus'; 'Zebrafish'; 'Abalone'; 'Bull'; ...
    'Human-low'; 'Human-high'};%;
chsn = [6,10:-1:7,1:5];

date = '200617A';
newdate = '200617B'; %These are 200617B in the included dataset
emptyonly = false;
fixset = [1:2, 4:10];

for sp = 1:length(specieslist)
    species = specieslist{sp};
    
    load([species '_' date '.mat']);
    
    
    
    dm = size(U_temp{1},1);
    idx = 1:dm:(dm*(length(U_temp)-1)+1);
    
    U_ = cat(3,U_temp{:});
    U_ = mean(U_,3);
    U = double(U_);
  
    sEnd = size(S_temp{1},1);
    for p =1:length(S_temp)
        try
            S_{p} = double(S_temp{p}(1:sEnd,1:sEnd));
        catch
            sz = size(S_temp{p});
            S_{p} = [S_temp{p}(1:sEnd,1:sz(2)), zeros(sEnd,sEnd-sz(2))];
        end
    end
    S_ = cat(3,S_{:});
    S_ = mean(S_,3);
    sv = diag(S_);
    S = double(cumsum(sv.^2) ./ sum(sv.^2));
    
    
    
    procdata_all{sp} = struct();
    procdata_all{sp}.S = S;
    procdata_all{sp}.U = U;
    clear S_ U_;
end


for sp = chsn%1:length(specieslist)
    species = specieslist{sp};
    load([species '_' date '.mat']);
    
    
    for i=1:length(procdata)
        goodidx(i) = ~isempty(procdata(i).A_overthresh);
    end
    
    
    for i=1:length(procdata)
        if goodidx(i)
            ind = i;
            
            if emptyonly
                try
                    if isempty(procdata(ind).wM_track_to_median)
                        selfDM = compareCurvatures(procdata,ind,procdata_all{sp},1);
                        for idx = 1:length(specieslist)
                            DMTracktoMedian(idx) = compareCurvatures(procdata,ind,procdata_all{idx},1);
                        end
                        
                        procdata(ind).wM_track_to_median = selfDM;
                        procdata(ind).DMTracktoMedian = DMTracktoMedian;
                        
                        
                        clear S_ U_;
                    end
                catch
                    selfDM = compareCurvatures(procdata,ind,procdata_all{sp},1);
                    for idx = 1:length(specieslist)
                        DMTracktoMedian(idx) = compareCurvatures(procdata,ind,procdata_all{idx},1);
                    end
                    
                    procdata(ind).wM_track_to_median = selfDM;
                    procdata(ind).DMTracktoMedian = DMTracktoMedian;
                    
                    
                    clear S_ U_;
                end
            else
                for idx = fixset%1:length(specieslist)
                    DMTracktoMedian(idx) = compareCurvatures(procdata,ind,procdata_all{idx},1);
                    procdata(ind).DMTracktoMedian(idx) = DMTracktoMedian(idx);
                end
                selfDM = compareCurvatures(procdata,ind,procdata_all{sp},1);
                procdata(ind).wM_track_to_median = selfDM;
                clear S_ U_;
            end
            
        end
    end
    
    
    save([species '_' newdate '.mat'],'A_overthresh', 'procdata', 'allruns','U_temp','thresh','kfolds','sv','S_temp');
end


%% Compare one cell track to another

clear all;


spec{1} = 'Spurpuratus';
spec{2} = 'Spurpuratus';

compdate{1} = '200617B';
compdate{2} = '200617B';

tracknum{1} = 1;
tracknum{2} = 63;


load([spec{1} '_' compdate{1} '.mat']);
proc{1} = procdata;

load([spec{2} '_' compdate{2} '.mat']);
proc{2} = procdata;

twoTrackDM = compareCurvatures(proc{1},tracknum{1},proc{2},tracknum{2})




%% Compare two different species to check that it's working

clear all;

spec{1} = 'Zebrafish';
spec{2} = 'Zebrafish';

compdate{1} = '200512';
compdate{2} = '200509';

for k=2:-1:1
    load([spec{k} '_' compdate{k} '.mat']);
    
    dm = size(U_temp{1},1);
    idx = 1:dm:(dm*(length(U_temp)-1)+1);
    
    %     UMode1 = cell2mat(U_temp);
    %     UMode2 = UMode1(:,idx+1);
    %     UMode1 = UMode1(:,idx);
    %
    %     U=[median(UMode1,2),median(UMode2,2)];
    
    U_temp = cat(3,U_temp{:});
    U_temp = mean(U_temp,3);
    U = double(U_temp);
    
    %SMode1 = cell2mat(S_temp);
    %SMode2 = SMode1(:,idx+1);
    %SMode3 = SMode1(:,idx+2);
    %SMode4 = SMode1(:,idx+3);
    %SMode1 = SMode1(:,idx);
    
    %S=[SMode1(1),SMode2(2),SMode3(3),SMode4(4)];
    sEnd = size(S_temp{1},1);
    for p =1:length(S_temp)
        try
            S_{p} = S_temp{p}(1:sEnd,1:sEnd);
        catch
            sz = size(S_temp{p});
            S_{p} = [S_temp{p}(1:sEnd,1:sz(2)), zeros(sEnd,sEnd-sz(2))];
        end
    end
    S_ = cat(3,S_{:});
    S_ = mean(S_,3);
    sv = diag(S_);
    S = double(cumsum(sv.^2) ./ sum(sv.^2));
    S = double(cumsum(sv.^2) ./ sum(sv.^2));
    
    proc{k} = struct();
    proc{k}.S = S;
    proc{k}.U = U;
    
    
    clear S_;
end

twospeciesDM = compareCurvatures(proc{1},1,proc{2},1);

figure;
plot(proc{1}.U(:,1)','-','Color','b'); hold on;
plot(proc{1}.U(:,2)','-','Color','r'); 
plot(proc{2}.U(:,1)','--','Color','b'); hold on;
plot(proc{2}.U(:,2)','--','Color','r');

ang = -0.15;
plot(proc{1}.U(:,1)'*cos(ang)+proc{1}.U(:,2)'*sin(ang)); hold on;
plot(proc{1}.U(:,2)'*cos(ang)-proc{1}.U(:,1)'*sin(ang)); 
